package com.acoffee.java2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author acoffee
 * @create 2021-11-09 21:12
 */
public class DynamicProxy implements InvocationHandler {

    private Girl girl;

    public DynamicProxy(Girl girl) {
        this.girl = girl;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        goBefore();
        Object obj = method.invoke(girl);
        goAfter();
        return obj;
    }


    public void goBefore(){
        System.out.println("化妆~~");
    }

    public void goAfter(){
        System.out.println("卸妆~~");
    }

    public Object getProxyInstance(){
        return Proxy.newProxyInstance(girl.getClass().getClassLoader(),girl.getClass().getInterfaces(),this);
    }
}
